package mulMan.gui.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import mulMan.domain.AudioFile;
import mulMan.domain.AudioFileFactory;
import mulMan.domain.Library;
import mulMan.gui.view.AddFileView;
import mulMan.gui.view.AudioFileView;
import mulMan.gui.view.LibraryView;
import mulMan.gui.view.ModifyFileView;
import mulMan.services.AudioFileServices;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class LibraryController{
	
	private Library library;
	private LibraryView view;
	private AudioFileServices audioFileServices;
	private ApplicationContext context = new ClassPathXmlApplicationContext(
			"application-context.xml");
	
	public Library getLibrary() {
		return library;
	}
	public void setLibrary(Library library) {
		this.library = library;
	}
	public AudioFileServices getAudioFileServices() {
		return audioFileServices;
	}
	public void setAudioFileServices(AudioFileServices audioFileServices) {
		this.audioFileServices = audioFileServices;
	}
	public LibraryView getView() {
		return view;
	}
	public void setView(LibraryView view) {
		this.view = view;
	}
	
	
	public LibraryController(Library library, LibraryView view) {
		this.library = library;
		this.view = view;
		audioFileServices = (AudioFileServices)context.getBean("audioFileServices");

		view.addModifyAudioFileListener(new ModifyAudioFileListener());
		view.addNewAudioFileListener(new NewAudioFileListener());
		view.addDeleteAudioFileListener(new DeleteAudioFileListener());
	}
	
	//ACTION LISTENERS
	
	class ModifyAudioFileListener implements MouseListener {

		public void mouseClicked(MouseEvent e) {
			if(e.getClickCount() == 2){
				int index = view.getListOfAudioFiles().locationToIndex(e.getPoint());
				
				AudioFile audioFile = library.get(index);
				AudioFileView modifyFileView = new ModifyFileView(audioFile);
	            new AudioFileController(audioFile, modifyFileView);
			}
		}

		public void mouseEntered(MouseEvent e) {}
		public void mouseExited(MouseEvent e) {}
		public void mousePressed(MouseEvent e) {}
		public void mouseReleased(MouseEvent e) {}
    }
	
	
	class NewAudioFileListener implements ActionListener {
	    public void actionPerformed(ActionEvent e) {
	    	AudioFileFactory audioFileFacory = new AudioFileFactory(library);
	    	
	    	AudioFile audioFile = audioFileFacory.newAudioFile();
	    	AudioFileView modifyFileView = new AddFileView(audioFile);
	    	new AudioFileController(audioFile, modifyFileView);
	    }
   }
	
	class DeleteAudioFileListener implements ActionListener {

		public void actionPerformed(ActionEvent arg0) {
			int index = view.getListOfAudioFiles().getSelectedIndex();
			try {
				audioFileServices.deleteAudioFile(library.get(index));
				library.remove(index);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
    }

}
